package com.sh4.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sh4.dao.impl.IfilesDaoImpl;
import com.sh4.entity.Ifiles;
import com.sh4.service.IfilesService;
import com.sh4.util.ContextHeader;

import net.sf.json.JSONObject;

@Service("ifilesServiceImpl")
@Transactional
public class IfilesServiceImpl implements IfilesService {

	@Autowired
	IfilesDaoImpl ifilesDaoImpl;

	@Override
	public String saveIfiles(JSONObject params) {

		Ifiles f = new Ifiles();
		f.setDeleteFlag("1");
		f.setHttpUrl(params.getString("httpUrl"));
		f.setLocalUrl(params.getString("localPath"));
		// f.setCreatedBy(ContextHeader.getUserCode());
		f.setCretedDate(new Date());
		f.setType("image");

		ifilesDaoImpl.saveOrUpdate(f);

		return f.getId();

	}

	@Override
	public Ifiles saveIfilesReturnIfile(JSONObject params) {

		Ifiles f = new Ifiles();
		f.setDeleteFlag("1");
		f.setHttpUrl(params.getString("httpUrl"));
		f.setLocalUrl(params.getString("localPath"));
		// f.setCreatedBy(ContextHeader.getUserCode());
		f.setCretedDate(new Date());
		f.setType("image");

		ifilesDaoImpl.saveOrUpdate(f);

		return f;

	}

	@Override
	public void updateIfiles(String fileIds, String linkId) {

		String[] ids = fileIds.split(",");
		for (String id : ids) {
			ifilesDaoImpl.executeUpdateBySql("update ifiles set LINK_ID_ = ? , UPDATE_DATE_ = now() where ID_ = ?",
					linkId, id);
		}

	}

	@Override
	public List<Ifiles> getIFiles(String linkId) {
		
		String hql = "from Ifiles where linkId = ? and deleteFlag = '1'";
		return ifilesDaoImpl.getList(hql, linkId);
	}
	
	

}
